/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rivercrossing;

import java.util.Collections;
import java.util.LinkedList;

/**
 *
 * @author Zie
 */
public class RiverCrossing {

    /*
     * Hi, today i'll present abount the river crossing pazzle ...
     * 
     * Rules
     * 1. The faster time is the better solution.
     * 2. Minimun passenger can crossing per round is 2
     * 3. Require at least 1 person to drive the boat.
     * 
     * Limitation
     * 1. Accept positive integer only. (input must be more that zero)
     * 
     * ok let's start
     */
    
    private static int max = 2;
    private static int total = 0;
    private static boolean isForward = true;
    public static void print (Integer x, Integer y) {
        /*
         * print function is used for display the result :)
         */
        if (x == null) x = new Integer(0);
        if (y == null) y = new Integer(0);
        int time = x > y ? x : y;
        System.out.println((isForward ? "[ GO ] " : "[BACK] ") + 
            (x > 0 ? x : "") +
            (y > 0 ? " " + y : "") +
            " -> " + time
            );
        total += time;
        isForward = !isForward;
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        // get input from command
        LinkedList<Integer> input = new LinkedList ();
        for (String i : args) {
            try {
                Integer value = Integer.parseInt(i);
                if (value < 0) continue;
                input.add(value);
            } catch (NumberFormatException e) {
                continue;
                /*
                 * create this for remove the unwanted input :)
                 */
            }
        }
        Collections.sort(input); // Sort the input from min to max :P
        Integer[] driver = new Integer[max];
        // pick 2 person from input to make a driver
        for (int i = 0; i < max; i++) {
            driver[i] = input.pollFirst();
        }
        while (!input.isEmpty()) { // Looping until all person are at the destination
            /*
             * First, move driver 2 to the destination :)
             */
            print(driver[0], driver[1]); // driver 1 and 2 go to destination
            print(driver[0], null); // driver 0 back
            Integer last = input.pollLast(); // pick first pessenger :P
            if (input.isEmpty()) {
                // for the last person
                print(driver[0], last); // go with driver 
                driver = null;
            } else {
                print(last, input.pollLast()); // go with next person
                print(driver[1], null); // driver 1 back
            }
        }
        // sometime driver is at the source
        if (driver != null) {
            // move them :)
            print(driver[0], driver[1]);
        }
        // display used time
        System.out.println("Total time is " + total);
        // OK testing ...
    }
}
